package makovo.controller;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.io.EOFException;

import org.apache.struts2.interceptor.SessionAware;

import makovo.data.Resources;
import makovo.entities.Address;
import makovo.entities.Applicant.ApplicantStep;

import com.opensymphony.xwork2.ActionSupport;

public class SocInfoRegister extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;

	private Map<String, Object> mSession;

	private String mTextPattern = "^[a-zA-Z0-9áéíóúÁÉÍÓÚàèìòùÀÈÌÒÙñÑ \\-.]{1,}$";
	private String mNumberPatter = "^[0-9.,]{1,}$";
	private String mOnlyNumberPatter = "^[0-9]{1,}$";

	private String curp;
	private String entName;
	private String ant;
	private String earn;
	private String entSector;
	private String job;
	private String dependants;

	private boolean sameAddress;
	private String state;
	private String city;
	private String loc;
	private String extNumber;
	private String intNumber;
	private String street;
	private String cp;

	private int stateId;
	private int cityId;
	private int locId;

	private String lat;
	private String lonng;
	private String col;
	public String getEntName() {
		return entName;
	}

	public void setEntName(String entName) {
		try {
			this.entName = new String(entName.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getCol() {
		return this.col;
	}

	public void setCol(String col) {
		this.col = col;
	}
	
	public String getAnt() {
		return ant;
	}

	public void setAnt(String ant) {
		this.ant = ant;
	}

	public String getEarn() {
		return earn;
	}

	public void setEarn(String earn) {
		this.earn = earn;
	}

	public String getEntSector() {
		return entSector;
	}

	public void setEntSector(String entSector) {
		try {
			this.entSector = new String(entSector.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		try {
			this.job = new String(job.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getDependants() {
		return dependants;
	}

	public void setDependants(String dependants) {
		this.dependants = dependants;
	}

	public boolean getSameAddress() {
		return sameAddress;
	}

	public void setSameAddress(boolean sameAddress) {
		this.sameAddress = sameAddress;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		try {
			this.state = new String(state.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		try {
			this.city = new String(city.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getLoc() {
		return loc;
	}

	public void setLoc(String loc) {
		try {
			this.loc = new String(loc.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getExtNumber() {
		return extNumber;
	}

	public void setExtNumber(String extNumber) {
		this.extNumber = extNumber.toUpperCase();
	}

	public String getIntNumber() {
		return intNumber;
	}

	public void setIntNumber(String intNumber) {
		this.intNumber = intNumber.toUpperCase();
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		try {
			this.street = new String(street.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getCp() {
		return cp;
	}

	public void setCp(String cp) {
		try {
			this.cp = new String(cp.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public int getStateId() {
		return this.stateId;
	}

	public void setStateId(int stateId) {
		this.stateId = stateId;
	}

	public int getCityId() {
		return this.cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public int getLocId() {
		return this.locId;
	}

	public void setLocId(int locId) {
		this.locId = locId;
	}

	public String getLat() {
		return this.lat;
	}

	public void setLat(String lat) {
		this.lat = lat;
	}

	public String getLonng() {
		return this.lonng;
	}

	public void setLonng(String lonng) {
		this.lonng = lonng;
	}

	public void validate() {
		if (!entName.matches(mTextPattern))
			addActionError("Nombre de la empresa");

		if (!ant.matches(mOnlyNumberPatter))
			addActionError("Antigüedad del aplicante");

		if (!earn.matches(mNumberPatter))
			addActionError("Ingreso mensual del aplicante");

		if (!job.matches(mTextPattern))
			addActionError("Puesto del aplicante");

		if (!dependants.matches(mOnlyNumberPatter))
			addActionError("Número de dependientes");

		if (!sameAddress) {
			if (!street.matches(mTextPattern))
				addActionError("Calle de la empresa");

			if (!extNumber.matches(mTextPattern))
				addActionError("Número externo");

			if (!intNumber.matches(mTextPattern))
				addActionError("Número interno");

			if (stateId == -1)
				addActionError("Estado");

			if (cityId == -1)
				addActionError("Ciudad");

			if (locId == -1)
				addActionError("Localidad");

			if (!cp.matches(mOnlyNumberPatter))
				addActionError("Código postal");
		}

	}

	public String execute() {
		int addressId = -1;

		try {
			if (sameAddress) {
				Address address = Resources.getApplicantInfoByCURP(curp).address;
				addressId = address.id;
			} else {
				if (locId == -1)
					loc = "";

				addressId = Resources.saveAddress(state, stateId, city, cityId, loc, locId, cp, street, extNumber, intNumber,
						Double.valueOf(lonng), Double.valueOf(lat),col);
			}

			if (addressId != -1) {
				boolean success = Resources.saveApplicantSocInfo(curp, addressId, Integer.valueOf(ant),
						Integer.valueOf(dependants), Integer.valueOf(entSector), entName, Double.valueOf(earn), job);
				if (success)
					success = Resources.changeApplicantStep(curp, ApplicantStep.PROJECT);

				if (success) {
					this.mSession.put("User.idSender", curp);
					return "SUCCESS";
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "ERROR";
	}

	@Override
	public void setSession(Map<String, Object> sessionInfo) {
		this.mSession = sessionInfo;
		curp = (String) mSession.get("User.idSender");
	}

}
